/*
 * @Description: 
 * @Version: 2.0
 * @Autor: lxc
 * @Date: 2020-12-18 17:21:22
 * @LastEditors: lxc
 * @LastEditTime: 2021-01-04 12:02:12
 */
const express = require("express");
const router = express.Router();
const Order = require("../../models/Order");
const passport = require("passport");



// $route GET api/orders/test
// @desc 返回请求的JSON数据
// @access public
router.get("/test", (req, res) => {
    res.json({ msg: 'longin works' });
})
// $route GET api/orders/getOrderList
// @desc 返回请求的JSON数据
// @access Private
router.get("/getOrderList", passport.authenticate('jwt', { session: false }), (req, res) => {
    const orderObj = {};
    if (req.query.expendName) orderObj.expendName = req.query.expendName;
    if (req.query.expendMoneyStart) orderObj.expendMoneyStart = req.query.expendMoneyStart;
    if (req.query.expendMoneyEnd) orderObj.expendMoneyEnd = req.query.expendMoneyEnd;
    if (req.query.classifyId) orderObj.classifyId = req.query.classifyId;
    if (req.query.pageIndex) orderObj.pageIndex = req.query.pageIndex;
    if (req.query.pageSize) orderObj.pageSize = req.query.pageSize;
    if (typeof req.query.date !== "undefined") {
        orderObj.date = req.query.date.split("@");
    }

    Order.find({ user: req.user.id }).then(orderTotal => {
        const cont = orderTotal.length
        Order.find({ user: req.user.id }).skip((orderObj.pageIndex - 1) * parseInt(orderObj.pageSize)).limit(parseInt(orderObj.pageSize))
            .where('expendName').regex(req.query.expendName)
            .then(order => {
                res.json({
                    dataList: order,
                    total: cont
                });
            })
    })

    //     const orderObj = {};
    //   orderObj.user = req.user.id;
    //   if(req.body.expendName) orderObj.expendName = req.body.expendName;
    //   if(req.body.expendMoneyStart) orderObj.expendMoneyStart = req.body.expendMoneyStart;
    //   if(req.body.expendMoneyEnd) orderObj.expendMoneyEnd = req.body.expendMoneyEnd;
    //   if(req.body.classifyId) orderObj.classifyId = req.body.classifyId;
    //   if(req.body.date) orderObj.date = req.body.date;
    // Order.find()
    // .where('expendName').regex(req.body.expendName)
    // .then(order => {

    // })
    // var query = Classify.find().where('name').regex('烫').then(classify => {
    //     res.json(classify);
    // })
    // var query = Classify.find().where('name').equals('抽烟').then(classify => {
    //     res.json(classify);
    // })
    // console.log(query);
})
// $route DELETE api/orders/delOrder
// @desc 删除开销订单
// @access Private
router.delete("/delOrder", passport.authenticate('jwt', { session: false }), (req, res) => {
    Order.deleteOne({ _id: req.body.id }).then(classify => {
        console.log(classify);
        if (classify.deletedCount) {
            res.json({ msg: '删除成功' });
        } else {
            res.json({ msg: '删除失败或订单不存在' });
        }
    }).catch(err => res.status(400).json(err))
    // let page = req.query.page
    // let limit = req.query.pageSize || 5
    // Classify.find({},function (err, data) {
    //     if(err) return res.status(500).json({
    //         result: 1,
    //         error_info: '请求失败！'
    //     })
    //     let count = data.length
    //     console.log(count)
    //     Classify.find({}).skip((page - 1)*parseInt(limit)).limit(parseInt(limit)).exec(function (err, data) {
    //         if(err) return res.status(500).json({
    //             result: 1,
    //             error_info: '服务器繁忙，请稍后重试！'
    //         })
    //         return res.status(200).json({
    //             result:0,
    //             message:'请求成功',
    //             total: count,
    //             artcleList: data
    //         })
    //     })
    // })
})

// $route POST api/orders/addOrders
// @desc 新增开销订单
// @access Private
router.post("/addOrders", passport.authenticate('jwt', { session: false }), async (req, res) => {
    let addObj = {
        expendName: req.body.expendName,
        expendMoney: req.body.expendMoney,
        classifyId: req.body.classifyId,
        user: req.user.id,
    }
    const Classify = require("../../models/Classify");

    await Classify.findOne({ _id: addObj.classifyId }).then(order => {
        addObj.classifyName = order.name
        const newOrder = new Order(addObj);
        newOrder.save().then(order => {

            res.json({ msg: '添加成功' });
        }, err => {
            console.log(err);
        })
    })
})

// $route POST api/orders/eidtOrder
// @desc 编辑开销订单
// @access Private
router.post("/eidtOrder", passport.authenticate('jwt', { session: false }), (req, res) => {
    let searchObj = {
        classifyId: req.body.classifyId,
        expendMoney: req.body.expendMoney,
        expendName: req.body.expendName,
        classifyName:req.body.classifyName,
        user: req.user.id
    }
    Order.findOneAndUpdate({ _id: req.body.id }).then(order => {
        if (!order) {
            return res.status(400).json({ msg: '没有该订单' })
        } else {
            const newOrder = new Order(searchObj);
            newOrder.save().then(order => {
                res.json({ msg: '编辑成功' });
            }, err => {
                console.log(err);
            })
        }
    })
})

module.exports = router;
